package com.it.sun.easychat.common.jdbctypehandler;

import cn.hutool.core.util.ObjectUtil;
import com.it.sun.easychat.model.vo.UserVO;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author sunmannian
 * @ClassName com.it.sun.easychat.common.jdbctypehandler.UserTypeHandler
 * @Date 2020/5/27 16:28
 * @Version V1.0
 * Description  <用户类型处理器>
 * Copyright (C) 北京中电兴发科技有限公司  since 2020/5/27
 */
@Component
@MappedTypes({UserVO.class})
public class UserTypeHandler extends BaseTypeHandler<UserVO> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, UserVO parameter, JdbcType jdbcType) throws SQLException {
        ps.setBytes(i, ObjectUtil.serialize(parameter));
    }

    @Override
    public UserVO getNullableResult(ResultSet rs, String columnName) throws SQLException {
        byte[] bys = rs.getBytes(columnName);
        if (null != bys) {
            return ObjectUtil.deserialize(bys);
        }
        return null;
    }

    @Override
    public UserVO getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        byte[] bys = rs.getBytes(columnIndex);
        if (null != bys) {
            return ObjectUtil.deserialize(bys);
        }
        return null;
    }

    @Override
    public UserVO getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        byte[] bys = cs.getBytes(columnIndex);
        if (null != bys) {
            return ObjectUtil.deserialize(bys);
        }
        return null;
    }
}
